// Copyright (C) 2012 Volkov Zachar
// This file is part of the "zAxis Game Engine".
// For conditions of distribution and use, see copyright notice zx_Common.hpp

#include "zx_Video.hpp"
#include "gl/glew.h"

namespace zx {

//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//
RenderBuffer::RenderBuffer( PixelFormat _format, uint _width, uint _height ) : m_format( _format ), m_width( _width ), m_height( _height ), m_handle( 0 )
{
     glGenRenderbuffersEXT( 1, &m_handle );
     glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_handle );
     glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, VideoDriver::_glTextureInternalFormat( TT_2D, m_format ), _width, _height );
     //glRenderbufferStorageMultisampleEXT( GL_RENDERBUFFER_EXT, 5, VideoDriver::_glTextureInternalFormat( TT_2D, (m_format = _format) ), (m_width = _width), (m_height = _height) );
     //glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, 0 );
     //glRenderbufferStorageMultisampleCoverageNV( GL_RENDERBUFFER_EXT, 8, 8, VideoDriver::_glTextureInternalFormat( TT_2D, m_format ), _width, _height );
     //int query;
     //glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &query);
     //printf( "samples = %d\n", query );
}
//----------------------------------------------------------------------------//
RenderBuffer::~RenderBuffer( void )
{
     glDeleteRenderbuffersEXT( 1, &m_handle );    
}
//----------------------------------------------------------------------------//
void RenderBuffer::Discard( PixelFormat _format, uint _width, uint _height )
{
     _Bind();
     //glRenderbufferStorageMultisampleEXT( GL_RENDERBUFFER_EXT, 4, VideoDriver::_glTextureInternalFormat( TT_2D, (m_format = _format) ), (m_width = _width), (m_height = _height) );
     glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, VideoDriver::_glTextureInternalFormat( TT_2D, (m_format = _format) ), (m_width = _width), (m_height = _height) );

            
     /*int query;
     glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &query);

     printf( "samples = %d\n", query );*/
}
//----------------------------------------------------------------------------//
void RenderBuffer::_Bind( void )
{
     GLint _current_buffer = 0;
     glGetIntegerv( GL_RENDERBUFFER_BINDING_EXT, &_current_buffer );

     if( _current_buffer != m_handle )
     {
          glBindBuffer( GL_RENDERBUFFER_EXT, m_handle );
     }
}
//----------------------------------------------------------------------------//
void RenderBuffer::_Unbind( void )
{
     GLint _current_buffer = 0;
     glGetIntegerv( GL_RENDERBUFFER_BINDING_EXT, &_current_buffer );

     if( _current_buffer == m_handle )
     {
          glBindBuffer( GL_RENDERBUFFER_EXT, m_handle );
     }
}
//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//

} // namespace zx
